{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d2de6f6a",
   "metadata": {},
   "source": [
    "## 定义一个函数\n",
    "* 函数代码块以 def 关键词开头，后接函数标识符名称和圆括号()。\n",
    "* 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。\n",
    "* 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。\n",
    "* 函数内容以冒号起始，并且缩进。\n",
    "* return [表达式] 结束函数，选择性地返回一个值给调用方。不带表达式的return相当于返回 None。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10391f6d",
   "metadata": {},
   "source": [
    "## 用“def”命名代码块\n",
    "* 利用函数可以便于管理代码，同时可以确保维护这个代码的一个副本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7a4b2202",
   "metadata": {},
   "outputs": [],
   "source": [
    "def searchvowels():\n",
    "# 用\"def\"关键字开始，为函数指定一个描述性好的名字\n",
    "# 提供一个可选的参数列表，在这里，这个函数还没参数，所以这个列表为空\n",
    "# 不要忘记冒号！\n",
    "    vowels = set('aeiou')\n",
    "    word = input(\"Provide a word to search for vowels:\")\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "41b2b5df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello\n",
      "e\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "searchvowels()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fe0e388",
   "metadata": {},
   "source": [
    "* 使用的字符串引号字符要保持一致，如果可以尽量使用单引号\n",
    "* 用单引号字符或者双引号字符包围的字符串不能跨多行：必须在同一个行上用一个匹配的引号字符结束这个字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31261cbc",
   "metadata": {},
   "source": [
    "## 函数可以接收参数\n",
    "* 增加参数：只需要把这个参数名插入到def行上的小括号中间，这个参数名将成为函数代码组的一个变量。\n",
    "* “代码组”是Python中对“块”的叫法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e3939b9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def searchvowels(word): # 将参数名放在小括号之间\n",
    "    \"\"\"docstring多行注释\"\"\"\n",
    "    vowels = set('aeiou') # 不再用input函数调用，一个我们不需要那行代码块。\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13d37163",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
